(function() {
	var homepage = true;
	var tab;
	// tabid计数器，保证tabid不会重复
	var tabidcounter = 0;

	$(document).ready(function() {
				var headerHeight = $("#header").outerHeight();
				var winH = $(window).height(), winW = $(window).width();
				var jsidebar = $("#sidebar"), jmaincontent = $("#maincontent");
				jsidebar.height(winH - headerHeight);
				jmaincontent.height(winH - headerHeight);
				jmaincontent.width(winW - jsidebar.outerWidth());

				// 布局初始化
				var bodyHeight = $(window).height() - 65;
				// tab
				var height = $(window).height() - 65;
				tab = $("#maincontent").ligerTab({
							height : height,
							showSwitchInTab : true,
							showSwitch : true
						});
					//初始化目录
					jQuery(function($) {
						loadTree(-1);
					});
					//加载 目录 ajax
					function loadTree(treeId) {
					 $.ajax({
							type:"post",
							url:baseUrl+'/NewMain/queryDemoList.do',
							data:"nodeId="+treeId,
							async:true,
							success:function(result){
								initMenus(result.data);
							}
						});
					}
					
					

				initMenuEvent();
				$("#pageloading").hide();
			});

	function initMenuEvent() {
		/**左边菜单点击**/
        $("#sidebar a.link").click(function (){
            var jlink = $(this);
			var tabid = jlink.attr("tabid");
            var url = jlink.attr("url");
            if (!url) return;
            if (!tabid){
                tabidcounter++;
                tabid = "tabid" + tabidcounter;
                jlink.attr("tabid", tabid); 
                if (url.indexOf('?') > -1) url += "&";
                else url += "?";
                url += "MenuNo=" + jlink.attr("menuno");
                jlink.attr("url", url);
            }
            openTab({
                tabid: tabid,
                text: jlink.text(),
                url: url
            });
        });
		
        $('#sidebar .sub-menu > a').click(function(){
            var last = $('.sub-menu.open', $('#sidebar'));
            console.log(last);
            last.removeClass("open");
            $('.arrow', last).removeClass("open");
            $('.sub', last).slideUp(200);
            var sub = $(this).next();
            if (sub.is(":visible"))
            {
                $('.arrow', $(this)).removeClass("open");
                $(this).parent().removeClass("open");
                sub.slideUp(200);
            }else{
                $('.arrow', $(this)).addClass("open");
                $(this).parent().addClass("open");
                sub.slideDown(200);
            }
        });

        $(".dropdown-toggle").click(function(){
            var isOpen = $(this).parent().hasClass("open");
            if (isOpen)
            {
                $(this).parent().removeClass("open");
            }
            else
            {
                $(this).parent().addClass("open");
            }
        });
	}
	function initMenus(allMenus) {
		var jtopmenu = $("#header .topmenu ul");
		var topMenus = [];
		for (var i = 0; i < allMenus.length; i++) {
			var menu = allMenus[i];
			menu.MenuNo = menu.MenuNo || ("menu" + ++tabidcounter);
			topMenus.push(menu);
		}

		$(topMenus).each(function(i, menu) {
			var jtopmenu_item = $('<li><img src="" /><span></span></li>')
					.appendTo(jtopmenu);
			jtopmenu_item.find("img").attr("src",menu.src);
			jtopmenu_item.find("span").html(menu.text);
			jtopmenu_item.attr("id", menu.id);
			jtopmenu_item.attr("data-index", i);
            var mainmenu = $('<ul class="sidebar-menu"><li class="sub-menu active"><a class="" href="javascript:void()"><i class="icon-dashboard"></i><span class="currentmenu"></span></a></li> </ul>').appendTo("#sidebar");
            if (i != 0) mainmenu.hide();
            mainmenu.find(".currentmenu").html(menu.text);
            mainmenu.attr("data-index", i);
			jtopmenu_item.click(function() {
				var index = $(this).attr("data-index");
                $("#sidebar .sidebar-menu").hide();
                $("#sidebar .sidebar-menu[data-index=" + index + "]").show();
						var id = $(this).attr("id");
						 $.ajax({
							type:"post",
							url:baseUrl+'/NewMain/queryDemoList.do',
							data:"nodeId="+id,
							async:true,
							success:function(result){
								leftChilden(result.data)
							}
						});
							
					});
			
			
		});

	}
	
	function leftChilden(allMenus){
		var mainmenu = $('<ul class="sidebar-menu"><li class="sub-menu active"><i class="icon-dashboard"></i><span class="currentmenu"></span></a></li> </ul>')
		.appendTo("#sidebar");
		var groups = getGroups(allMenus);
            $(groups).each(function(gi,group){
				var titleG = groups[gi].title;
            	var item_html = $('<li class="sub-menu" ><a href="javascript:;" class=""><span>' + titleG.text + '</span><span class="arrow"></span></a><ul class="sub"></ul></li>')
            	.appendTo(mainmenu);
				item_html.attr("id", titleG.id);
				item_html.click(function(){
					var id = $(this).attr("id");
					$.ajax({
							type:"post",
							url:baseUrl+'/NewMain/queryDemoList.do',
							data:"nodeId="+id,
							async:true,
							success:function(result){
								console.info(result.date);
								threeChild(result.data,item_html);
							}
						});
					
				});
			});
		
		
	}
	
	function threeChild(allMenus,item_html){
			var mainmenu = item_html.find(".sub");
			mainmenu.empty();
			var groups = getGroups(allMenus);
            $(groups).each(function(gi,group){
				var titleG = groups[gi].title;
				console.info(titleG.url);
            	var item_html = $('<li><a class="link" href="'+titleG.url+'">'+titleG.text+'</a></li>').appendTo(mainmenu);
				item_html.attr("id", titleG.id);
				return;
			});
			//$(".sub").hide();
			mainmenu.toggle();
	}
	
	
	
	
	
	/*function quary(id){
		$.ajax({
				type:"post",
				url:baseUrl+'/NewMain/queryDemoList.do',
				data:"nodeId="+id,
				async:true,
				success:function(result){
					console.info(result.data);
				}
			});
	}*/
	 function getGroups(items){
        	var groups = [];
            var groupTitles = [];
            for(var i = 0;i<items.length;i++){
            	var item = items[i];
            	if(item){
            		groupTitles.push(item);
            	}
            }
            
            for (var i = 0; i < groupTitles.length; i++){
                var groupTitle = groupTitles[i];
                groups.push({
                    title: groupTitle,
                    items: getGroupItems(groupTitle)
                });
            }
            function getGroupItems(groupTitle){
                var gitems = [];
                for (var i = 0; i < items.length; i++){
                    var item = items[i];
                    if (item.children == groupTitle)
                    {
                        gitems.push(item);
                    }
                }
                return gitems;
            }
            return groups;
        }

	window.callFrame = function(frameName, fnName) {
		var ele = $("#" + frameName);
		console.log(ele.attr("src"));
		if (!ele.length)
			return;
		ele = ele[0];
		if (!ele.contentWindow || !ele.contentWindow[fnName])
			return;
		ele.contentWindow[fnName]();
	}
	window["openTab"] = function(options, tabid, text) {
		if (typeof(options) == "string") {
			options = {
				url : options,
				tabid : tabid,
				text : text
			};
		}
		var p = options || {};
		p.url = ne.getAppUrl(p.url);
		var tabid = p.tabid, text = p.text || p.title, url = p.url;
		if (tab.isTabItemExist(tabid)) {
			var tabTitle = tab.getTabItemTitle(tabid), tabSrc = tab
					.getTabItemSrc(tabid);
			if (tabTitle != text) {
				tab.setTabItemTitle(tabid, text);
			}
			if (tabSrc != url) {
				tab.setTabItemSrc(tabid, url);
			}
			tab.selectTabItem(tabid);
			return;
		}
		tab.addTabItem(p);
	};
})();
